﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Data;
using System.IO;

namespace HY.WEB.SA
{
    public partial class comp_add : PageBase
    {
        public string us,um,ubh;
        string tab = "company";
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();



            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["stype"]))
                {
                    hidstype.Text = Request["stype"].Trim();
                }
                fill();

        

                ExtAspNet.PageContext.RegisterStartupScript
                     (
                     "var d=Ext.getCmp('" + ddlcomp.ClientID + "');" +
                     " var oldInput=\"\";" +
                     " d.listWidth=500; " +
                     " d.addListener(\"specialkey\",function(f,e){ " +  //keyup
                     " var newInput=document.getElementById(\"" + ddlcomp.ClientID + "\").value;" +
                      " if (oldInput==newInput) {return;} " +
                     " oldInput=newInput;" +
                      " __doPostBack('loaddata',newInput);" +
                      "});"
                     );  

            }
            else
            { 
            
             if (Request["__EVENTTARGET"] == "loaddata")  
              {                                          

              string sqltxt="SELECT * FROM company where charindex('"+
                  Request["__EVENTARGUMENT"].ToString()+"',cname)>0 ";
              txtnm.Text = Request["__EVENTARGUMENT"].ToString(); 
              
                 DataTable dt =DAL.DbHelperSQL.SqlDataAdapter(sqltxt);  
                ddlcomp.Items.Clear();  
                for (int i=0;i<dt.Rows.Count;i++)
                  {  
                     ddlcomp.Items.Add(new ExtAspNet.ListItem(dt.Rows[i]["cname"].ToString(),dt.Rows[i]["cname"].ToString()));  
                   }  
                
             
               //下面这句话是有道理的，extjs默认效果在autocomplete之后，值为变成下拉框的第一项，有点破坏效果，这里纠正过来，可以输入多个关键字  
                 ExtAspNet.PageContext.RegisterStartupScript("document.getElementById(\"" + ddlcomp.ClientID + "\").value='" +
                          Request["__EVENTARGUMENT"] + "';");
             
             }  


            }

     
        }


         protected override void Render(HtmlTextWriter writer)  
       {  
            StringWriter strWriter = new StringWriter();  
            base.Render(new HtmlTextWriter(strWriter));  
            writer.Write(  
                strWriter.ToString()
              .Replace("id: \"" + 
              ddlcomp.ClientID + "\",", "id: \"" + 
              ddlcomp.ClientID + "\",editable: true,hideTrigger:true,")  
               );  
             //偷天换日效果，替换extjs字符串，加入2个属性，这2个属性在后期改的话我测试会出错，可能写法不对，就直接写在这里，分别是让下拉框可以编辑，并且隐藏右边的下拉按钮  

          }

     void   fill()
      {
           
         //行业分类
         DataTable e = BLL.Bllbase.get_table("[guest_hy]");
         ddlhy.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlhy.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlhy.DataBind();
            ddlhy.Items.Add(new ExtAspNet.ListItem("", ""));


            //客户分类
             e = BLL.Bllbase.get_table("[guest_type]");
             ddltype.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddltype.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddltype.DataBind();
            ddltype.Items.Add(new ExtAspNet.ListItem("", ""));

            //客户性质
            e = BLL.Bllbase.get_table("[guest_xz]");
            ddlxz.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlxz.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlxz.DataBind();
            ddlxz.Items.Add(new ExtAspNet.ListItem("", ""));

            //区域
            e = BLL.Bllbase.get_table("[pub_area]");
            ddlcity.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcity.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["svalue"].ToString(), e.Rows[i]["scode"].ToString()));
            }
            ddlcity.DataBind();

            //业务级别
            e = BLL.Bllbase.get_table("[sys_level]");
            ddllevel.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddllevel.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["names"].ToString(), e.Rows[i]["names"].ToString()));
            }
            ddllevel.DataBind();

            //地区
            ddlcity.SelectedIndex = ddlcity.Items.IndexOf(ddlcity.Items.FindByText("广东省"));
            ddlhy.SelectedIndex = ddlhy.Items.IndexOf(ddlhy.Items.FindByText("工业"));
            ddlxz.SelectedIndex = ddlhy.Items.IndexOf(ddlhy.Items.FindByText(""));

      }
  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
          Model.CO.company CP=new Model.CO.company();

            CP.cbh=BLL.BllAutoCode.GetMaxBH("company");
            if(CP.cbh=="") return;

            if(txtnm.Text!="")
            if (BLL.Bllbase.check_exits("company","cname='"+txtnm.Text.Trim()+"'"))
            {
                Alert.Show("公司名已经存在"); return;
            }

            CP.cname = txtnm.Text;

            if (String.IsNullOrEmpty(date1.Text.Trim()))
            {
                date1.Text = "1900-01-01";
            }
             
            CP.reg_date=DateTime.Parse(date1.Text.Trim());
          
            CP.cntype=ddlxz.SelectedValue;
          
            CP.addresss=txtaddress.Text;

            CP.city = ddlcity.SelectedValue;

            CP.level = ddllevel.SelectedValue;

            CP.person=txtperson.Text;
            CP.sex = ddlsex.SelectedValue;
            CP.job = txtjob.Text;
            CP.ctel = txttel.Text;
            CP.mtel = txtmtel.Text;
            CP.qq = txtqq.Text;
            CP.msn = txtmsn.Text;


            CP.compnum = txtcompnum.Text;

            
            if (txttel.Text != "")
                if (BLL.Bllbase.check_exits("company", "ctel='" + txttel.Text.Trim() + "'"))
                {
                    Alert.Show("公司电话已经存在，请检查用户信息是否重复。"); return;
                }

  


            
            CP.cfax=txtfax.Text;
            CP.post = txtpost.Text;
            CP.website=txtwebsite.Text;
            CP.email=txtmail.Text;
      
            CP.cnum=0;
            CP.ht=0;

            CP.salestype=ddltype.SelectedValue;

            CP.cstatus=0;

            CP.hytype = ddlhy.SelectedValue;

            CP.memo= HttpUtility.HtmlEncode(FCKeditor1.Value);

            BLL.CO.company CY = new BLL.CO.company();
            CY.Add(CP);

            BLL.co.pub_tel mt = new BLL.co.pub_tel();
            Model.co.pub_tel mtadd = new Model.co.pub_tel();
            mtadd.bh = CP.cbh;
            mtadd.bhname = CP.cname;

            mtadd.person = txtperson2.Text;
            mtadd.sex = ddlsex2.SelectedValue;
            mtadd.job = txtjob2.Text;
            mtadd.ctel = txttel2.Text;
            mtadd.mtel = txtmtel2.Text;
            mtadd.qq = txtqq2.Text;
            mtadd.msn = txtmsn2.Text;
            mt.Add(mtadd);

            mtadd.bh = CP.cbh;
            mtadd.bhname = CP.cname;
            mtadd.person = txtperson3.Text;
            mtadd.sex = ddlsex3.SelectedValue;
            mtadd.job = txtjob3.Text;
            mtadd.ctel = txttel3.Text;
            mtadd.mtel = txtmtel3.Text;
            mtadd.qq = txtqq3.Text;
            mtadd.msn = txtmsn3.Text;
            mt.Add(mtadd);

            //更新业务员操作记录
            BLL.Bllbase.update_rec(tab,um," cbh='"+CP.cbh+"' ");

                      

            ////写入收藏表
            Alert.Show(BLL.BllSales.intosales("企业", CP.cbh,CP.cname,ubh,um,"CP"));

            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(hidstype.Text) + ActiveWindow.GetHidePostBackReference());
        }

       
    }
}